package Corpus;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

import DataObjects.Article;
import Main.Pair;
import Main.String2;
import Main.Threesome;

public class DemoCorpus implements Corpus{

	private static String[] ids={"0305","0308","0569","0594","0620"};
	private String corpusPath;

	private class MyIterator implements Iterator<Pair<Article,String>>{

		private int index=0;

		@Override
		public boolean hasNext() {
			return index<ids.length-1;
		}

		private Article makeArticle(String name){
			name=name+".txt";
			String title="";
			LinkedList<Pair<String,Object>> metadata=new LinkedList<Pair<String,Object>>();
			File textFile=new File(corpusPath+"\\Text\\"+name);
			File metadataFile=new File(corpusPath+"\\metadata\\"+name);
			try {

				FileReader reader=new FileReader(metadataFile);
				BufferedReader buf=new BufferedReader(reader);

				String line=buf.readLine();
				while(line!=null && !line.trim().equals("")){
					String[] splited=line.split(" == ");
					if (splited[0].equals("title")){
						title=splited[1];
					}
					else if (splited[1]==null || splited[1].equals("null")){
						metadata.add(new Pair<String, Object>(splited[0],"Unknown"));           
					}
					else if (splited[0].equals("cat")){
						if (splited[1].equals("[]")){
							metadata.add(new Pair<String, Object>(splited[0],"Unknown"));           
						}
						else{
							Vector<String> cats=String2.split(splited[1].substring(1, splited[1].length()-1),", ");
							for(String cat : cats){
								metadata.add(new Pair<String, Object>(splited[0], cat));
							}
						}
					}
					else{
						metadata.add(new Pair<String, Object>(splited[0], splited[1]));                 
					}
					line=buf.readLine();
				}

				buf.close();
				reader.close();

				String text="";
				reader=new FileReader(textFile);
				buf=new BufferedReader(reader);
				line=buf.readLine();
				while(line!=null){
					text+=line+"\n";
					line=buf.readLine();
				}                               
				if (text.trim().equals(""))
					System.out.println("text id: "+Integer.parseInt(String2.split(name, ".").get(0)));
				buf.close();
				reader.close();
				return new Article(Integer.parseInt(String2.split(name, ".").get(0)) ,title, text, metadata);
			} catch (FileNotFoundException e) {
				e.printStackTrace();//TODO do samthing or dont do samthing
			} catch (IOException e) {
				e.printStackTrace();//TODO do samthing or dont do samthing
			}

			return null;                    
		}

		@Override
		public Pair<Article,String> next() {                    
			Article ans=makeArticle(ids[index]);
			index++;
			return new Pair<Article, String>(ans, corpusPath+"\\Text\\"+ids[index]+".txt");
		}

		@Override
		public void remove() {
			// TODO Auto-generated method stub      
		}

	}

	public DemoCorpus(String corpusPath){
		this.corpusPath=corpusPath;
	}

	@Override
	public Vector<Threesome<String, String, MetadataType>> getAllMetadataNames() {
		Vector<Threesome<String,String,MetadataType>> ans=new Vector<Threesome<String,String,MetadataType>>();

		ans.add(new Threesome<String,String,MetadataType>("author","Author",MetadataType.string));
		ans.add(new Threesome<String,String,MetadataType>("last_modified","Date",MetadataType.date));
		ans.add(new Threesome<String,String,MetadataType>("cat","Category",MetadataType.array));//TODO

		return ans;
	}

	@Override
	public String getArticlesPath() {
		return this.corpusPath+"\\Text\\";
	}

	@Override
	public Iterator<Pair<Article, String>> getIteratorForTheArticles() {
		return new MyIterator();
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

}
